package com.cyf.plc.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.GridLayoutManager
import com.cyf.library.util.logi
import com.cyf.plc.R
import com.cyf.plc.adapter.RoomAdapter
import com.cyf.plc.adapter.SpaceItemDecoration
import com.cyf.plc.base.BaseFragment
import com.cyf.plc.bean.PanelStatus
import com.cyf.plc.constant.Constants
import com.cyf.plc.constant.Constants.Companion.ATTR_HUMIDITY
import com.cyf.plc.constant.Constants.Companion.ATTR_POWER
import com.cyf.plc.constant.Constants.Companion.ATTR_SET_TEMP
import com.cyf.plc.constant.Constants.Companion.ATTR_TEMP
import com.cyf.plc.constant.Constants.Companion.PANEL_STATUS
import com.cyf.plc.constant.Constants.Companion.PANEL_ATTR_IDS
import com.cyf.plc.constant.Constants.Companion.ROOMS
import com.cyf.plc.databinding.FragmentPlcRoomBinding
import com.cyf.plc.model.PlcRoom
import com.cyf.plc.viewmodel.PlcViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@ExperimentalCoroutinesApi
class PlcRoomFragment : BaseFragment<PlcViewModel, FragmentPlcRoomBinding>(),
    RoomAdapter.OnItemClickListener {

    private val panelStatus = PanelStatus(0f, 0, false, 0f)

    override fun layoutId(): Int = R.layout.fragment_plc_room

    override fun createViewModel(): PlcViewModel = activity?.run {
        ViewModelProvider(this)[PlcViewModel::class.java]
    } ?: throw Exception("Invalid Activity")

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun getPlcRooms(plcRooms: List<PlcRoom>) {
        updateRooms(plcRooms)
    }

    private val mAdapter: RoomAdapter by lazy { RoomAdapter(onItemClickListener = this) }

    override fun initView() {
        // Initialize RecyclerView
        mViewBinding.rvRoom.apply {
            layoutManager = GridLayoutManager(this.context, 3)
            adapter = mAdapter
            addItemDecoration(SpaceItemDecoration(68))
        }

        arguments?.run {
            getParcelableArrayList<PlcRoom>(ROOMS).let {
                if (it != null) {
                    mAdapter.submitList(it.toMutableList())
                }
            }
        }
    }

    fun updateRooms(plcRooms: List<PlcRoom>) {
        mAdapter.submitList(plcRooms.toMutableList())
    }

    override fun onItemClicked(plcRoom: PlcRoom, view: View) {
        val plcAttributeNewIds = ArrayList<String>()
        for (plcAttributeId in PANEL_ATTR_IDS) {
            plcAttributeNewIds.add(plcRoom.roomId.toString() + Constants.UNDERLINE + plcAttributeId)
        }
        mViewModel.getPlcAttributes(plcAttributeNewIds.toTypedArray()).observe(viewLifecycleOwner,
            Observer { it ->
                "List:$it".logi()
                it.forEach {
                    when (it.attributeName) {
                        ATTR_SET_TEMP -> {
                            panelStatus.setTempPlcAttribute = it
                        }
                        ATTR_TEMP -> {
                            panelStatus.tempPlcAttribute = it
                        }
                        ATTR_POWER -> {
                            panelStatus.powerPlcAttribute = it
                        }
                        ATTR_HUMIDITY -> {
                            panelStatus.humidityPlcAttribute = it
                        }
                    }
                }
                val bundle = Bundle()
                bundle.putParcelable(PANEL_STATUS, panelStatus)
                NavHostFragment.findNavController(this)
                    .navigate(R.id.action_main_fragment_to_plc_temp_fragment, bundle)
            })
    }

    override fun onItemLongClick(plcRoom: PlcRoom, view: View) {

    }


}